﻿using System;
using System.Collections.Generic;
using HYCodeBase.Math;

namespace HYCodeBase.CommonUtility.CompareUtility
{
	public class DoubleEqualityComparer : IEqualityComparer<double>
	{
		public double UseTolerance
		{
			get
			{
				return this.useTolerance;
			}
			private set
			{
				this.useTolerance = value;
			}
		}

		public void SetPrecision(uint input)
		{
			this.m_nUsePrecision = Convert.ToInt32(input);
			this.UseTolerance = this.CalculateTolerance();
		}

		public string GetDoubleString(double obj)
		{
			string format = this.m_strBaseString + this.m_nUsePrecision;
			return obj.ToString(format);
		}

		public bool Equals(double x, double y)
		{
			return MathUtility.IsEqual(x, y, this.UseTolerance);
		}

		public int GetHashCode(double obj)
		{
			return this.GetDoubleString(obj).GetHashCode();
		}

		private double CalculateTolerance()
		{
			return 1.0 / System.Math.Pow(10.0, (double)this.m_nUsePrecision);
		}

		private int m_nUsePrecision = 4;

		private string m_strBaseString = "F";

		private double useTolerance = 0.0001;
	}
}
